import Util;

import RPC;
import EndpointUtil;

extends RPC;

init(config: RPC.Config){
  super(config);
  @endpointRule = 'regional';
  
  checkConfig(config);
  @endpoint = getEndpoint('viapiutils', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

model GetOssStsTokenRequest = {
}

model GetOssStsTokenResponse = {
  requestId: string(name='RequestId'),
  data: {
    accessKeyId: string(name='AccessKeyId'),
    accessKeySecret: string(name='AccessKeySecret'),
    securityToken: string(name='SecurityToken'),
    script: string(name='Script'),
  }(name='Data'),
}

async function getOssStsTokenWithOptions(request: GetOssStsTokenRequest, runtime: Util.RuntimeOptions): GetOssStsTokenResponse {
  Util.validateModel(request);
  return doRequest('GetOssStsToken', 'HTTPS', 'POST', '2020-04-01', 'AK', null, request, runtime);
}

async function getOssStsToken(request: GetOssStsTokenRequest): GetOssStsTokenResponse {
  var runtime = new Util.RuntimeOptions{};
  return getOssStsTokenWithOptions(request, runtime);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }

  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}
